Skip to content

feat: per-instance random source for deterministic behaviour#8

Merged
basgys merged 1 commit intomainfrom
feat/per-instance-random-source
Mar 15, 2026
Merged

feat: per-instance random source for deterministic behaviour#8
basgys merged 1 commit intomainfrom
feat/per-instance-random-source

Conversation

@basgys
Copy link
Copy Markdown
Contributor

@basgys basgys commented Mar 15, 2026

Summary

  • WithRandomSource(src rand.Source) — new per-instance option that controls the random draw used to decide whether a request is shed. Follows the same pattern as WithNow and WithRejectedErrorFunc. Falls back to the global rand.Float64() when not set. The source must be safe for concurrent use if the throttle is used concurrently (same contract as the rest of the API — no implicit wrapping).

  • math/randmath/rand/v2 — the package now uses the v2 API, available since Go 1.22 (our minimum version).

  • Deterministic tests — two fixed sources (alwaysShed, neverShed) replace every probabilistic assertion in the test suite. Tests that previously relied on "shed rate should be > 30%" or "P(all forwarded) < 0.01%" now assert exact outcomes. Also replaces the two real-time integration tests (TestAdaptiveThrottleRecovery, TestAdaptiveThrottleMinimumRate) with clock- and formula-based equivalents, and adds TestRejectionProbabilityFormula and TestRejectionProbabilityPriorityCrossContamination to verify the formulas directly.

Add WithRandomSource(src rand.Source) to allow per-instance control of
the random sampling used to make shed/pass decisions. The source must be
safe for concurrent use if the throttle is used concurrently. Follows
the same pattern as WithNow and WithRejectedErrorFunc.

Switch the package from math/rand to math/rand/v2.

Update tests to use two fixed sources — alwaysShed (Uint64 returns 0)
and neverShed (Uint64 returns MaxUint64) — replacing probabilistic
assertions with exact ones throughout.
@basgys basgys force-pushed the feat/per-instance-random-source branch from 6db2104 to cc7ce14 Compare March 15, 2026 15:37
@basgys basgys merged commit b6d9f66 into main Mar 15, 2026
2 checks passed
@basgys basgys deleted the feat/per-instance-random-source branch March 15, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant